package com.entor.hqj.main;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.entor.hqj.login.R;

import java.util.List;

/**
 * Created by Clock on 2016/08/01.
 */
public class AuthorRecyclerAdapter extends RecyclerView.Adapter<AuthorRecyclerAdapter.AuthorViewHolder> {

    private Context context;

    private List<AuthorInfo> mAuthorInfoList;

    public AuthorRecyclerAdapter() {
    }

    public AuthorRecyclerAdapter(List<AuthorInfo> mAuthorInfoList,Context context) {
        this.mAuthorInfoList = mAuthorInfoList;
        this.context = context;
    }


    @Override
    public AuthorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View childView = inflater.inflate(R.layout.author_card_layout, parent, false);
        AuthorViewHolder viewHolder = new AuthorViewHolder(childView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(AuthorViewHolder holder, int position) {
        if (mAuthorInfoList != null) {
            AuthorInfo authorInfo = mAuthorInfoList.get(position);
            holder.mNickNameView.setText(authorInfo.getNickName());
            holder.mMottoView.setText(authorInfo.getMotto());
            holder.id = authorInfo.getId();
            holder.vi_id = authorInfo.getVi_id();
            holder.result = authorInfo.getResult();
            holder.candidate = authorInfo.getCandidate();
        }

    }

    @Override
    public int getItemCount() {
        return mAuthorInfoList != null ? mAuthorInfoList.size() : 1;
    }

    class AuthorViewHolder extends RecyclerView.ViewHolder {

        TextView mNickNameView;
        TextView mMottoView;
        Integer id;
        String result;
        Long vi_id;
        String candidate;
        public AuthorViewHolder(View itemView) {
            super(itemView);
            mNickNameView = (TextView) itemView.findViewById(R.id.tv_nickname);
            mMottoView = (TextView) itemView.findViewById(R.id.tv_motto);
            mNickNameView.setOnClickListener(new FinishiListenner());

        }

        class FinishiListenner implements View.OnClickListener{

            @Override
            public void onClick(View v) {
                System.out.println(mNickNameView.getText().toString()+"-----"+result);
                Intent  intent = new Intent();
                intent.putExtra("id",id);
                intent.putExtra("vi_id",vi_id);
                intent.putExtra("candidate",candidate);
                if(result.equals("1"))
                    intent.setClass(context,FinishVoterActivity.class);
                else
                    intent.setClass(context,NotFinishVoterActivity.class);
                context.startActivity(intent);
            }
        }

    }
}
